// https://juejin.cn/post/6844904194575433735?from=search-suggest
// https://blog.csdn.net/chaoPerson/article/details/135594904）


// 简单的策略模式实现
const orderType = 1 // 1: 美妆，2：电器，3：家具

const orderFunction = {
  1() { console.log('美妆订单') },
  2() { console.log('电器订单') },
  3() { console.log('家具订单') },
}

orderFunction[orderType]()



// 多重嵌套业务
const orderWay = 2 // 1：h5，2：app，3：小程序

const strategy = () => { // 订单类型+环境类型策略
  const map = new Map([
    [{
      orderType: 1,
      orderWay: 1
    }, () => {
      console.log('美妆订单h5')
    }],
    [{
      orderType: 1,
      orderWay: 2
    }, () => {
      console.log('美妆订单app')
    }],
    [{
      orderType: 1,
      orderWay: 3
    }, () => {
      console.log('美妆订单小程序')
    }],
    [{
      orderType: 2,
      orderWay: 1
    }, () => {
      console.log('电器订单h5')
    }],
    [{
      orderType: 2,
      orderWay: 2
    }, () => {
      console.log('电器订单app')
    }],
    [{
      orderType: 2,
      orderWay: 3
    }, () => {
      console.log('电器订单小程序')
    }],
    [{
      orderType: 3,
      orderWay: 1
    }, () => {
      console.log('家具订单h5')
    }],
    [{
      orderType: 3,
      orderWay: 2
    }, () => {
      console.log('家具订单app')
    }],
    [{
      orderType: 3,
      orderWay: 3
    }, () => {
      console.log('家具订单小程序')
    }],
  ])
  return map
}

const run = (orderType, orderWay) => {
  let action = [...strategy()].filter(([key, value]) => (key.orderType === orderType && key.orderWay === orderWay))
  action.forEach(([key, value]) => value.call(this))
}

run(orderType, orderWay)





// 条件也是方法
const describeMapArrary = [
    [
        (name) => name === 'teacher', // 判断条件
        () => {
          // 业务逻辑处理...
          console.log('teacher',"我是学校的老师") // 执行函数
        }
    ],
    [
        (name) => name === 'chef', 
        () => {
           // 业务逻辑处理...
          console.log('chef',"我是酒店的大厨")
        }
    ],
    [
        (name) => name === 'soldier', 
        () => {
          // 业务逻辑处理...
          console.log('soldier',"我是保家卫国的军人")
        }
    ],
    [
        (name) => name === 'driver', 
        () => {
          // 业务逻辑处理...
          console.log('driver',"我是滴滴司机");
          return 'driver 我是driver场景的返回值'; // 有返回值场景
        }
    ],
];
    
function getDescribeName(name) {
    // 获取符合条件的子数组
    const getDescribe = describeMapArrary.find((item) => item[0](name));
    // 子数组存在则运行子数组中的第二个元素（执行函数）
    return getDescribe ? getDescribe[1]() : console.log('default',"没找到你需要的职位描述！");
}
getDescribeName('soldier'); // 查询map中存在的条件
console.log(getDescribeName('driver')); // 查询map中存在的条件并返回函数处理值
getDescribeName('fuchaoyang'); // 查询map中不存在的条件
